package com.ita.os.client.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.jni.Global;

import com.ita.os.client.global.GlobalVar;
import com.ita.os.client.po.Merchant;
import com.ita.os.client.po.Page;
import com.ita.os.client.service.MerchantService;
import com.ita.os.client.service.impl.MerchantServiceImpl;
import com.ita.os.client.util.PageUtil;
import com.ita.os.client.util.StringUtil;

/**
 * Servlet implementation class ListMerchantServlet
 */
@WebServlet("/index")
public class LoadMerchantServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantService merchantService = new MerchantServiceImpl();
	
    public LoadMerchantServlet() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String curPage = request.getParameter("curPage");
		String type = request.getParameter("type");
		if(StringUtil.isEmpty(curPage)){
			curPage="1";
		}
		Page p=new Page(Integer.parseInt(curPage),12);
		List<Merchant> mList = merchantService.listMerchants(p,type);
		int total=merchantService.countMerchant(type);
		String pageCode=PageUtil.getPagation(request.getContextPath()+"/index?", total, Integer.parseInt(curPage), 12);
		request.setAttribute("imgPath", GlobalVar.imgPath);
		request.setAttribute("mList", mList);
		request.setAttribute("pageCode", pageCode);
		request.getRequestDispatcher("/home.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
